package org.nicocube.airain.admin.client;

import java.util.Map;

import org.nicocube.airain.common.client.AirainMessenger;
import org.nicocube.airain.domain.client.gamedate.GameDate;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AirainAdmin implements EntryPoint {

	public interface I18nConstants extends Constants{
		@DefaultStringValue("Beginning")
		String beginningLabel();

		@DefaultStringValue("Last")
		String lastLabel();

		@DefaultStringValue("Current")
		String currentLabel();

		@DefaultStringValue("Loading...")
		String loading();
	}

	class GameDateCallback implements AsyncCallback<Map<String,GameDate>>{

		private final AirainMessenger messenger;
		private final Label labelBeginning;
		private final Label labelLast;
		private final Label labelCurrent;

		public GameDateCallback(AirainMessenger messenger, Label labelBeginning,
				Label labelLast, Label labelCurrent) {
			this.messenger = messenger;
			this.labelBeginning = labelBeginning;
			this.labelLast = labelLast;
			this.labelCurrent = labelCurrent;
		}

		@Override
		public void onFailure(Throwable caught) {
			messenger.show("Error !");
		}

		@Override
		public void onSuccess(Map<String,GameDate> result) {
			labelBeginning.setText(String.valueOf(result.get("beginning")));
			labelLast.setText(String.valueOf(result.get("last")));
			labelCurrent.setText(String.valueOf(result.get("current")));
		}
	}

	
	private AdminServiceAsync adminService;
	private I18nConstants constants;
	private AirainMessenger messenger;

	@Override
	public void onModuleLoad() {
		VerticalPanel v = new VerticalPanel();
		
		RootPanel.get().add(v);

		adminService = GWT.create(AdminService.class);
		constants = GWT.create(I18nConstants.class);
		
		messenger = new AirainMessenger();
		
		messenger.show(constants.loading());
		
		v.add(gameDateManagerView());

		v.add(gameCharacterListView());
		
		messenger.hide();
	}


	private Widget gameDateManagerView() {
		final Grid g0 = new Grid(3,3);
		DecoratorPanel d0 = new DecoratorPanel();
		d0.add(g0);
		
		g0.setText(0, 0, constants.beginningLabel());
		g0.setText(0,1, ":");
		Label label0 = new Label();
		g0.setWidget(0, 2, label0);

		g0.setText(1, 0, constants.lastLabel());
		g0.setText(1,1, ":");
		Label label1 = new Label();
		g0.setWidget(1, 2, label1);

		g0.setText(2, 0, constants.currentLabel());
		g0.setText(2,1, ":");
		Label label2 = new Label();
		g0.setWidget(2, 2, label2);
		adminService.retrieveX(new GameDateCallback(messenger,label0,label1,label2));
		
		return d0;
	}

	private Widget gameCharacterListView() {
		DecoratorPanel d0 = new DecoratorPanel();
		return d0;
	}
}
